<?php 
require_once OUTER_PATH."auth/User.php";
class user_helper
{
	/**
	 * 
	 * 获取登录跳转链接
	 * @return string
	 */
	public static function getLoginURL ()
    {
        $currentUrl = 'http://' . $_SERVER['HTTP_HOST'] .
         $_SERVER['REQUEST_URI'];
        $delimiters = (strpos($currentUrl, "?") ==
         FALSE) ? '?' : '&';
        $currentUrl = $currentUrl . $delimiters .
         'url=' . urlencode($currentUrl);
        return urlencode($currentUrl);
    }
    /**
     * 
     * 初始化用户信息, 保存在$controller对象里 userInfo array(
     * 	urealname=>用户名,
     *  uid=>数字帐号,
     *  ptid=>pt帐号
     * )
     * @param object $controller
     * @return void
     */
	public static function initUserInfo(&$controller)
	{
		$uinfo = &User::getAuthInfo();
        if ($uinfo ==
         null) {
            $controller->userInfo["urealname"] = "游客";
            $controller->userInfo["uid"] = 0;
            $controller->userInfo["uname"] = "";
        } else {
            $controller->userInfo["urealname"] = $uinfo["originid"];
            if ($controller->userInfo['urealname'] == '') {
                $controller->userInfo['urealname'] = $uinfo['ptid'];
            }
            $controller->userInfo["uid"] = $uinfo["sdid"];
            $controller->userInfo["ptid"] = $uinfo["ptid"];
        }
	}
	/**
	 * 
	 * 提示跳转
	 * @param string $message 错误提示
	 * @param string $goto 跳转链接
	 * @param object $controller 
	 * @return void
	 */
	public static function tips($message, $goto = null, $controller)
	{
        if (!$goto) {
        	$goto = base_url();
        }
		$data["message"] = $message;
        $data["goto"] = $goto;
        $controller->load->view("basic_tips", $data);
        echo ($controller->output->get_output());
        exit();
	}
	/**
	 * 
	 * 检测是否登录
	 * @param $controller
	 * @param 是否未登录情况返回值 $isReturn false不返回值，直接跳转到登录页面
	 * @return boolean
	 */
    public static function checkLogin ($controller, $isReturn=false)
    {
        if (! User::getAuthInfo()) {
        	if ($isReturn) {
        		return true;
        	}
            $currentUrl = self::getLoginURL();
            self::tips(
            "对不起，该操作需要登录，现在为您转向登录页面", 
            'https://cas.sdo.com/cas/login?service=' .
             $currentUrl, $controller);
        } else {
            return true;
        }
    }
    
    /**
     * 获取内部帐号用户信息
     */
    public function getUserinfoByUserName($username)
    {
		$username	= iconv("UTF-8", "GB18030", $username);
    	$url		= "http://61.172.241.94:8084/Tivoli/SsoDetailInfo?code=";
    	$url		.= $username;
    	$ret		= file_get_contents($url);
    	if (!$ret)
    		return false;
		$ret	= iconv("GB18030","UTF-8",$ret);
    	$row	= explode('|', $ret);
    	$info	= array();
    	$info['realname']	= $row[2];
    	$info['department']	= $row[4].$row[5];
    	$info['email']		= $row[7];
    	return $info;
    }
}
